var G = {};
G.canvas = {
	container: null,
	zIndex: 1000,
	id: 0,
	focusDiv: null,
	isInit: false,	//判断浏览器是否已装载完毕
	config: {
		width: 0,	//容器宽
		height: 0,	//容器高
		step: 3		//线的延伸长度
	},
	/**
	 * 初始化，默认在document.onload中运行
	 */
	init: function(){
		this.container = document.getElementById('canvas');
		$(this.container).height($(document).height());
		this.initVMLGroup();
		
		document.oncontextmenu = function(e){return false};	//禁用右键菜单
		document.onselectstart = function(e){return false};
		//Drag.init(G.canvas.container, G.canvas.vGroup);
		
		var tree = new TreeView();
		tree.loadXML();
		this.tree = tree;
		
		this.adjusetGroupSize();
	},
	/**
	 * 动态装载js类
	 */
	loadJS : function(url, charset) {
		if (!charset) {
	        charset = "UTF-8";
	    }
	    var charsetProperty = " charset=\"" + charset + "\" ";
	    document.write("<script type=\"text/javascript\" src=\"" + url + "\" onerror=\"alert('Error loading ' + this.src);\"" + charsetProperty + "></script>");
	},
	/**
	 * 初始化v:group容器
	 */
	initVMLGroup: function() {
		var width = $(this.container).width();
		var height = $(this.container).height();
		this.config.width = width;
		this.config.height = height;
		this.vGroup = document.createElement('<v:group id="vGroup" coordorigin="0,0" style="z-index:1;position:absolute;left:0px;top:0px;width:'+width+';height:'+height+'" coordsize="'+width+','+height+'"/>');
		this.container.appendChild(this.vGroup);
		
		var lastMouseX, lastMouseY;
		var moveRunning = false;
		this.container.onmousedown = function() {
			//if (event.button != 2) return false;
			lastMouseX = event.clientX;
			lastMouseY = event.clientY;
			moveRunning = true;
		};
		this.container.onmousemove = function() {
			if (moveRunning && event.button != 0) {
				this.scrollLeft = this.scrollLeft + lastMouseX - event.clientX;
				lastMouseX = event.clientX;
				this.scrollTop = this.scrollTop + lastMouseY - event.clientY;
				lastMouseY = event.clientY;
			}
		};
		this.container.onmouseup = function() {
			moveRunning = false;
		}
		document.onomouseout = function() {
			moveRunning = false;
		}
	},
	/**
	 * 再容器装载完毕后修正vGroup的大小
	 */
	adjusetGroupSize: function() {
		var leftDiv = document.getElementById('leftDiv');
		var topDiv = document.getElementById('topDiv');
		var _this = this;
		if (this.isInit) {
			var h = $(this.vGroup).height();
			var w = $(this.vGroup).width();
			var t = parseInt(topDiv.style.top);
			var l = parseInt(leftDiv.style.left);
			if (t < h) topDiv.style.top = h + 1000;
			if (l < w) leftDiv.style.left = w + 1000;
			
			this.vGroup.style.top = (parseInt(topDiv.style.top) - h) / 2;
			this.vGroup.style.left = (parseInt(leftDiv.style.left) - w) / 2;
			$(this.container).scrollTop($(this.tree.root.getUI()).offset().top - (document.body.clientHeight / 2));
			$(this.container).scrollLeft($(this.tree.root.getUI()).offset().left - parseInt(document.body.clientWidth / 5));
			
		} else setTimeout(function(){_this.adjusetGroupSize();_this = null;}, 200);
		
	},
	/**
	 * 得到全局z坐标值
	 */
	getZIndex: function() {
		this.zIndex++;
		return this.zIndex;
	},
	/**
	 * 得到全局ID值
	 */
	getID: function() {
		this.id++;
		return this.id;
	},
	/**
	 * 初始化时装载动态脚本，注意：此类必须在head区域运行
	 */
	initOutputScript: function() {
		this.loadJS('core/CoreClass.js');
		this.loadJS('core/ListenerProxy.js');
		this.loadJS('core/Component.js');
		this.loadJS('core/Shape.js');
		this.loadJS('core/LineView.js');
		this.loadJS('core/RoundRect.js');
		this.loadJS('core/TreeView.js');
	},
	
	initFocusDiv : function() {
		var div = document.createElement('<div id="focusDiv" class="focus-div" style="height:100%;width:100%;"></div>');
		this.container.appendChild(div);
		
		div.show = function(node) {
			if (this.isShow) return;
			var offset = $(node.getUI()).offset();
			this.style.left = parseInt(offset.left + $(G.canvas.container).scrollLeft());
			this.style.top = parseInt(offset.top + $(G.canvas.container).scrollTop());
			this.style.width = parseInt(node.getUI().style.width) + 7;
			this.style.height = parseInt(node.getUI().style.height) + 7;
			this.style.display = 'block';
			this.isShow = true;
		};
		div.hide = function(node) {
			this.style.display = 'none';
			this.isShow = false;
		};
		div.onclick = function() {
			
		};
		this.focusDiv = div;
	},
	
	getFocusDiv : function() {
		if (this.focusDiv == null) this.initFocusDiv();
		return this.focusDiv;
	}
};